package com.taoli.service.impl;

import com.taoli.entity.BalanceDetail;
import com.taoli.entity.Wallet;
import com.taoli.repository.BalanceDetailRepository;
import com.taoli.service.BalanceDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;


@Service
public class BalanceDetailServiceImpl implements BalanceDetailService {
    @Autowired
    private BalanceDetailRepository balanceDetailRepository;

    /**
    * @Description: 保存余额明细记录
    * @author: 韦武朝
    * @date: 2023/2/16 20:36
    * @param balanceDetail: 余额明细实体
    * @Return: com.taoli.entity.BalanceDetail
    */
    @Override
    public BalanceDetail save(BalanceDetail balanceDetail) {
        return balanceDetailRepository.save(balanceDetail);
    }

    /**
    * @Description: 根据用户id分页查询明细记录
    * @author: 为武朝
    * @date: 2023/2/16 20:35
    * @param userId: 用户ID
    * @param pageSize: 页大小
    * @param pageNumber: 页码
    * @Return: org.springframework.data.domain.Page<com.taoli.entity.BalanceDetail>
    */
    @Override
    public Page<BalanceDetail> getBalanceDetailList(Long userId, Integer pageSize, Integer pageNumber) {
        Sort sort = Sort.by(new Sort.Order(Sort.Direction.DESC, "detailId"));
        Pageable  pageable = PageRequest.of(pageNumber-1, pageSize, sort);
        Specification specification = (root, criteriaQuery, criteriaBuilder)->{
            Predicate predicate = criteriaBuilder.equal(root.get("userId"), userId);
            return predicate;
        };
        Page<BalanceDetail> pageDate = balanceDetailRepository.findAll(specification,pageable);
        return pageDate;
    }
}
